fish <- read.csv("fish.csv") %>%
drop_na(location.long,location.lat, utm.easting, utm.northing
)
qaqc_plot <- ggplot() +
geom_jitter(data=fish,
aes(utm.easting,utm.northing,
color=individual.local.identifier),
width = 50,
height = 50,
size = 3, alpha = 0.7) +
labs(x="Easting", y="Northing") +
guides(color=guide_legend("Identifier"))
plotly::ggplotly(qaqc_plot)
Gosh, that looks awful. But its fine, it doesn’t need to be perfect! Let’s work on some analyses now.
lapply(split(fish, fish$individual.local.identifier),
function(x)write.csv(x, file = paste(x$individual.local.identifier[1],".csv", sep = ""), row.names = FALSE))
files <- list.files(path = ".", pattern = "\\.csv$", full.names = TRUE)
ny <- "ny.tif"
image_stack <- raster::stack(ny)
crop_extent <- raster::extent(min(fish$utm.easting-2000),
max(fish$utm.easting+2000),
min(fish$utm.northing-2000),
max(fish$utm.northing+2000))
crop_full <- raster::extent(min(fish$utm.easting-5000),
max(fish$utm.easting+5000),
min(fish$utm.northing-5000),
max(fish$utm.northing+5000))
image_crop <- crop(image_stack, crop_extent)
image_full <- crop(image_stack, crop_full)
new_york <- as.data.frame(image_crop, xy = TRUE) %>% setNames(c("x", "y", "red", "green", "blue"))
new_york.full <- as.data.frame(image_full, xy = TRUE) %>% setNames(c("x", "y", "red", "green", "blue"))
ggplot()+geom_spatial_rgb(data = new_york, aes(x=x, y=y, r=red, g=green, b=blue))+coord_sf()
Here we will be focusing on an individual by using different techniques. The first one will be by using a minimum convex polygon.
## Data for a single individual
F1 <- read.csv('F1.csv')
xy <- c(as.data.frame(F1$utm.easting),as.data.frame(F1$utm.northing))
## projecting the data to UTM
data.proj <- SpatialPointsDataFrame(xy,F1, proj4string = CRS("+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))
xy.sp <- SpatialPoints(data.proj@coords)
## MCP analysis
mcp.out <- mcp(xy.sp, percent=100, unout="ha")
## converting mcp to sf object for ggplot and points to df
mcp.sf <- st_as_sf(mcp.out)
mcp.points <- cbind((data.frame(xy)),F1$individual.local.identifier)
colnames(mcp.points) <- c("x","y", "identifier")
# MCP plot
mcp.plot <- ggplot() + theme_bw() +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) +
theme(panel.grid = element_blank()) +
geom_spatial_rgb(data = new_york, aes(x=x, y=y, r=red, g=green, b=blue)) +
geom_sf(data=mcp.sf, alpha = 0.1) +
geom_point(data=mcp.points, aes(x=x, y=y), color = "hotpink") +
labs(x="Easting (m)", y="Northing (m)", title=mcp.points$identifier) +
theme(legend.position="none", plot.title = element_text(face = "bold", hjust = 0.5)) +
coord_sf(
xlim = range(new_york$x),
ylim = range(new_york$y),
expand = FALSE
)
mcp.plot + annotate("text", x = mean(mcp.points$x), y = max(mcp.points$y) + 100, label = paste(round(mcp.out@data$area,2),"ha"),
fontface = "bold", size = 5, color = "white")
Now we will try essentially the same thing but this time using kernel-density estimation
## Data for a single individual
kde_raster <- function(filename){
data <- read.csv(file = filename)
data <- data %>%
filter(!is.na(utm.easting), !is.na(utm.northing))
x <- as.data.frame(data$utm.easting)
y <- as.data.frame(data$utm.northing)
xy <- c(x,y)
## projecting the data to UTM
data.proj <- SpatialPointsDataFrame(xy,data, proj4string = CRS("+proj=utm +zone=18 +datum=WGS84 +units=m +no_defs"))
xy.sp <- SpatialPoints(data.proj@coords)
## KDE analysis at 95, 85, 75
kde<-kernelUD(xy.sp, h="href", kern="bivnorm", grid=100)
ver95 <- getverticeshr(kde, 95)
ver85 <- getverticeshr(kde, 85)
ver75 <- getverticeshr(kde, 75)
## converting to sf object and df
kde.points <- cbind((data.frame(data.proj@coords)),data$individual.local.identifier)
colnames(kde.points) <- c("x","y","identifier")
kde95.sf <- st_as_sf(ver95)
kde85.sf <- st_as_sf(ver85)
kde75.sf <- st_as_sf(ver75)
bb <- sf::st_bbox(kde95.sf) #make a bounding box to zoom in
##making the plot
kde.plot <- ggplot() + theme_bw() +
theme(axis.text.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5)) +
geom_spatial_rgb(data = new_york, aes(x=x, y=y, r=red, g=green, b=blue)) +
geom_sf(data=kde95.sf, fill = "hotpink", alpha = 0.4) +
geom_sf(data=kde85.sf, fill = "purple", alpha = 0.3) +
geom_sf(data=kde75.sf, fill = "orange", alpha = 0.2) +
geom_point(data=kde.points, aes(x=x, y=y), color = "lightgray") +
labs(x="Easting (m)", y="Northing (m)", title=kde.points$identifier) +
theme(legend.position="none", plot.title = element_text(face = "bold", hjust = 0.5)) +
coord_sf(
xlim = c(bb["xmin"], bb["xmax"]),
ylim = c(bb["ymin"], bb["ymax"]),
expand = FALSE)
kde.plot + annotate("text", x = (bb["xmin"] + bb["xmax"])/2, y = (bb["ymax"] - 50), label = paste("95% ",round(ver95$area,2),"ha"),
fontface = "bold", size = 4, color = "white")
}
pblapply(files, kde_raster)
## [[1]]
##
## [[2]]
##
## [[3]]
##
## [[4]]
##
## [[5]]
##
## [[6]]
##
## [[7]]
##
## [[8]]
##
## [[9]]
Slay! I successfully finished this assignment. (It was so difficult plz
Dr. Gentry & Dr. Gienger don’t make me do this again this was so
stressful.)